﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using DoAnOOP.BussinessLayer;


namespace DoAnOOP.DataAccessLayer
{
    class clsUserLogin
    {
        private SqlDataAdapter da1, da2, da3;
        private DataSet ds1, ds2, ds3;
        
        public clsUserLogin(string _username, string _password)
        {
            clsConnect myConnect = new clsConnect();
            string a = clsConnect.ConnectionString;
            
            da1 = new SqlDataAdapter("SELECT * FROM TAIKHOANSINHVIEN WHERE MSSV = '" + _username + "' AND MATKHAU = '" + _password + "'", clsConnect.ConnectionString);
            da2 = new SqlDataAdapter("SELECT * FROM TAIKHOANGV WHERE MAGIAOVIEN = '" + _username + "' AND MATKHAU = '" + _password + "'", clsConnect.ConnectionString);
            da3 = new SqlDataAdapter("SELECT * FROM ADMIN WHERE ID = '" + _username + "' AND PASS = '" + _password + "'", clsConnect.ConnectionString);
            ds1 = new DataSet();
            ds2 = new DataSet();
            ds3 = new DataSet();
            
            da1.Fill(ds1);
            da2.Fill(ds2);
            da3.Fill(ds3);
        }
        public bool checkLogin()
        {
            
            
            if (ds3.Tables[0].Rows.Count != 0)
            {
                DataRow daRow = ds3.Tables[0].Rows[0];
                clsBusUserLogin.username = daRow["ID"].ToString().Trim();
                clsBusUserLogin.password = daRow["PASS"].ToString().Trim();
                clsBusUserLogin.QuyenHan = "admin";
                return true;
            }
            else if (ds1.Tables[0].Rows.Count != 0)
            {
                DataRow daRow = ds1.Tables[0].Rows[0];
                clsBusUserLogin.username = daRow["MSSV"].ToString();
                clsBusUserLogin.password = daRow["MATKHAU"].ToString();
                clsBusUserLogin.QuyenHan = "SV";
                return true;
            }
            else if (ds2.Tables[0].Rows.Count != 0)
            {
                DataRow daRow = ds2.Tables[0].Rows[0];
                clsBusUserLogin.username = daRow["MAGIAOVIEN"].ToString().Trim();
                clsBusUserLogin.password = daRow["MATKHAU"].ToString().Trim();
                clsBusUserLogin.QuyenHan = "GV";
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
